package cn.wolfcode.luowowo.util;

import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.RegionQuery;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

public class DBHelper {

    //抽取出来
    public static <T> Page<T> query(MongoTemplate mongoTemplate,Class<T> clz,
                                    Pageable pageable,Query query) {
        //获取count
        long count = mongoTemplate.count(query,clz);
        if(count == 0){
            return Page.empty();
        }

        query.with(pageable);
        //获取list
        List<T> list = mongoTemplate.find(query, clz);

        return new PageImpl<T>(list,pageable,count);
    }
}
